//
//  LCLoginService.m
//  lechuang
//
//  Created by Mr_hi on 2023/3/24.
//  Copyright © 2023 Mooyi. All rights reserved.
//

#import "LCLoginService.h"
#import "LCUserModel.h"


@implementation LCLoginService

// 发送验证码
+(void)sendSessionCodeWithMobStr:(NSString *)mobStr CodeType:(NSString *)codetype Block:(void(^)(BOOL isSuccess))block {
    NSMutableDictionary *param = [NSMutableDictionary new];
    param[@"CaptchaModel"] = codetype;
    param[@"UserMobile"] = mobStr;
    NSString *force = [LCFunction ns_md5:[NSString stringWithFormat:@"短信验证码-|-|-%@-|-|-短信验证码",mobStr]];
    param[@"CaptchaKey"] = force;
    
    [[XWXNetWorkQuery sharedManage] AFRequestData:Url(URLCaptcha) HttpMethod:KPost HaveToken:NO params:param completionHandle:^(id  _Nonnull result) {
        if ([[result objectForKey:@"success"] boolValue]) {
            block(YES);
        } else {
            [self showErrorText:[result objectForKey:@"tips"]];
            block(NO);
        }
    } errorHandle:^(NSError * _Nonnull error) {
        XWXNetError
        block(NO);
    }];
}

/// 登陆
/// @param mobStr 手机号
/// @param pswd 密码
/// @param block 登陆成功的回调
+ (void)loginWithMobStr:(NSString *)mobStr Password:(NSString *)pswd Block:(void(^)(BOOL isSuccess))block {
    NSMutableDictionary * param = [NSMutableDictionary dictionary];
    param[@"SourceTerminal"] = @"APP";
    param[@"DeviceType"] = @"iOS";
    param[@"DeviceCode"] = [LCDeviceTool getIdfa];
    param[@"PhoneModel"] = [LCDeviceTool getDeviceModel];
    param[@"SystemEdition"] = [LCDeviceTool deviceVersion];
    param[@"Username"] = mobStr;
    param[@"Password"] = pswd;
    param[@"isPassword"] = @"1";
    
    [[XWXNetWorkQuery sharedManage] AFRequestData:Url(URLLogin) HttpMethod:KPost HaveToken:NO params:param completionHandle:^(id  _Nonnull result) {
        if ([[result objectForKey:@"success"] boolValue]) {
            LCUserModel * userInfo = [LCUserModel mj_objectWithKeyValues:result[@"field"]];
            LCUserDefSet(userInfo.mj_keyValues, UserInfo);
            LCUserDefSet(userInfo.userthumb, UserImg);
            LCUserDefSet(userInfo.nickname, UserName);
            LCUserDefSet(userInfo.usertokey, klogin_cre);
            LCUserDefSet(userInfo.userid, Userid);
            block(YES);
        } else {
            [self showErrorText:[result objectForKey:@"tips"]];
            block(NO);
        }
    } errorHandle:^(NSError * _Nonnull error) {
        XWXNetError
        block(NO);
    }];
}

/// 注册
/// @param mobStr 手机号
/// @param pswd 密码
/// @param code 验证码
/// @param block 注册成功的回调
+ (void)registerWithMobStr:(NSString *)mobStr Pswd:(NSString *)pswd Captcha:(NSString *)code ShareCode:(NSString *)shareCode Block:(void(^)(BOOL isSuccess))block {
    NSMutableDictionary * param = [NSMutableDictionary dictionary];
    param[@"SourceTerminal"] = @"APP";
    param[@"DeviceType"] = @"iOS";
    param[@"DeviceCode"] = [LCDeviceTool getIdfa];
    param[@"PhoneModel"] = [LCDeviceTool getDeviceModel];
    param[@"SystemEdition"] = [LCDeviceTool deviceVersion];
    
    param[@"Username"] = mobStr;
    param[@"UserMobile"] = mobStr;
    param[@"Password"] = pswd;
    param[@"isCaptcha"] = @"1";
    param[@"Captcha"] = code;
    param[@"ShareCode"] = shareCode;
    
    [[XWXNetWorkQuery sharedManage] AFRequestData:Url(URLRegister) HttpMethod:KPost HaveToken:NO params:param completionHandle:^(id  _Nonnull result) {
        if ([[result objectForKey:@"success"] boolValue]) {
            LCUserModel * userInfo = [LCUserModel mj_objectWithKeyValues:result[@"field"]];
            LCUserDefSet(userInfo.mj_keyValues, UserInfo);
            LCUserDefSet(userInfo.userthumb, UserImg);
            LCUserDefSet(userInfo.nickname, UserName);
            LCUserDefSet(userInfo.usertokey, klogin_cre);
            LCUserDefSet(userInfo.userid, Userid);
            block(YES);
        } else {
            [self showErrorText:[result objectForKey:@"tips"]];
            block(NO);
        }
    } errorHandle:^(NSError * _Nonnull error) {
        XWXNetError
        block(NO);
    }];
}

/// 验证短信验证码
/// @param mobStr 手机号
/// @param code 验证码
/// @param block 验证成功的回调
+ (void)verificationWithMobStr:(NSString *)mobStr Captcha:(NSString *)code Block:(void(^)(BOOL isSuccess, NSString * usertokey))block {
    NSMutableDictionary * param = [NSMutableDictionary dictionary];
    param[@"Username"] = mobStr;
    param[@"Captcha"] = code;
    
    [[XWXNetWorkQuery sharedManage] AFRequestData:Url(URLVerification) HttpMethod:KPost HaveToken:NO params:param completionHandle:^(id  _Nonnull result) {
        if ([[result objectForKey:@"success"] boolValue]) {
            NSString * usertokey = result[@"field"][@"usertokey"];
            block(YES, usertokey);
        } else {
            [self showErrorText:[result objectForKey:@"tips"]];
            block(NO, @"");
        }
    } errorHandle:^(NSError * _Nonnull error) {
        XWXNetError
        block(NO, @"");
    }];
}

/// 重置密码
/// @param mobStr 手机号
/// @param usertokey 登陆标识
/// @param pswd 密码
/// @param surePswd 确认密码
/// @param block 请求完成的回调
+ (void)resetPswdWithMobStr:(NSString *)mobStr Usertokey:(NSString *)usertokey Pswd:(NSString *)pswd SurePswd:(NSString *)surePswd Block:(void(^)(BOOL isSuccess))block {
    
    NSMutableDictionary * param = [NSMutableDictionary dictionary];
    param[@"Username"] = mobStr;
    param[@"UserTokey"] = usertokey;
    param[@"Password"] = pswd;
    param[@"SurePassword"] = surePswd;
    
    [[XWXNetWorkQuery sharedManage] AFRequestData:Url(URLResetPswd) HttpMethod:KPost HaveToken:NO params:param completionHandle:^(id  _Nonnull result) {
        if ([[result objectForKey:@"success"] boolValue]) {
            block(YES);
        } else {
            [self showErrorText:[result objectForKey:@"tips"]];
            block(NO);
        }
    } errorHandle:^(NSError * _Nonnull error) {
        XWXNetError
        block(NO);
    }];
}

@end
